<?php namespace Home\Controller; 
use Think\Controller;

/**
 * Class 登录控制器
 * @package Home\Controller
 */
class LoginController extends CommonController
{

	public function index()
	{
		$question =D('Verify')->getOneVerify();

        $this->assign('question',$question);

        
        $this->display();

	}

	public function checkdate()
	{
		//POST 	获取人类验证
		$answer = $_POST['answer'];
		//判断人类验证
		if ($answer!=$_SESSION['answer']) {
			$arr = array(
    			'error'=>1,
    			'info'=>'人类验证失败，请重新输入！'
    			);
    		echo json_encode($arr);
    		exit;
		}

		//POST 	获取邮箱
		$email = $_POST['email'];
		//账号转小写
		$email =strtolower($email);

		//POST 	获取密码
		$password = $_POST['password'];
		$password = md5($password);

		//检测邮箱和密码
		$sql="select * from user where email='{$email}' and password='{$password}'";
		$res=M('user')->query($sql);
		//判断sql结果
		if ($res) {	
			$_SESSION['answer'] ='';
			$res=current($res);
			if ($res['ustate'] ==1) {
				$arr = array(
	    			'error'=>2,
	    			'info'=>'账号正在停用状态，请联系管理员！'
	    			);    			
		    		echo json_encode($arr);
		    		exit;
			}elseif($res['ustate']==2) {
				$arr = array(
	    			'error'=>3,
	    			'info'=>'账号己删除，无法登录！'
	    			);    			
		    		echo json_encode($arr);
		    		exit;
			}else{
				$_SESSION['id']=$res['id'];
		 		$_SESSION['nickname']=$res['nickname'];
		 		$_SESSION['face']=$res['face'];		
				$arr = array(
    			'error'=>0,
    			'info'=>'登录成功！即将跳转到会员中心！'
    			);    			
	    		echo json_encode($arr);
	    		exit;
			}
		}else{
			$arr = array(
    			'error'=>1,
    			'info'=>'账号或密码错误，请重新输入！'
    			);
	    		echo json_encode($arr);
	    		exit;
		}

	}



		public function LoginOut()
		{
			session_unset();
			session_destroy();
			$this->success('退出成功',U('home/login/index'));
		}



		public function qqreturn()
    	{
	        $openid=getOpenid();

	        if($res = M('user')->where(array('openid'=>$openid))->find()){
	            $_SESSION['id']=$res['id'];
	            $_SESSION['nickname']=$res['nickname'];
	            $_SESSION['face']=$res['face'];
	            $this->success('登录成功',U('home/index/index'));
	        }else{
	            $userInfo = getUserInfo();
	            $data['nickname']= $userInfo['nickname'];
	            $data['openid']= $openid;
	            $data['shen']=$userInfo['province'];
	            $data['city']= $userInfo['city'];
	            $data['create_time']= time();
	            if($res = M('user')->add($data)){
	                $_SESSION['id']=$res;
	                $_SESSION['nickname']=$data['nickname'];
	                $this->success('登录成功',U('home/index/index'));
	            }
	        }
    	}

		public function qqlogin()
		{
		    bbslogin();
		}
        











}